﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using Newtonsoft.Json;

namespace NF_Base
{
    /// <summary>
    /// 根据用户输入地址，获取经纬度信息
    /// </summary>
    public class Getlonglat
    {

        private string Ak = "bKEdmTeH5GWtcypqxFBNakE3";

        public Getlonglat()
        {
            this.Url = "http://api.map.baidu.com/location/ip?ak=" + Ak;
        }


        public Getlonglat(string region, string address)
        {
            this.Region = region;
            this.Address = address;
            this.OutPut = "json";
            this.Url = "http://api.map.baidu.com/place/v2/suggestion?query=" + Address + "&region=" + Region +
                       "&output=" + OutPut + "&ak=" + Ak;
        }

        /// <summary>
        /// 用户输入的搜索关键字
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 查询的城市
        /// </summary>
        public string Region { get; set; }

        /// <summary>
        /// 返回数据的格式
        /// </summary>
        public string OutPut { get; set; }

        /// <summary>
        /// 请求的Url
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 获取经纬度（get）
        /// </summary>
        /// <param name="type">编码方式</param>
        public Rootobject GetlonglatInfo()
        {
            Rootobject root = new Rootobject();
            try
            {
                root = JsonConvert.DeserializeObject<Rootobject>(HttpRequestApi(Url));
            }
            catch (Exception ex)
            {
                ErroLog.SysLog(ex);
            }
            return root;
        }


        public CityRoot GetCity()
        {
            CityRoot root = new CityRoot();
            try
            {
                root = JsonConvert.DeserializeObject<CityRoot>(HttpRequestApi(Url));
            }
            catch (Exception ex)
            {
                ErroLog.SysLog(ex);
            }
            return root;
        }

        /// <summary>
        /// 发送http请求 get
        /// </summary>
        public string HttpRequestApi(string url)
        {
            string jsonstr = string.Empty;
            try
            {
                WebRequest wreqest = WebRequest.Create(url);
                WebResponse wresponse = wreqest.GetResponse();
                Stream respstream = wresponse.GetResponseStream();
                using (StreamReader reader = new StreamReader(respstream, Encoding.GetEncoding("utf-8")))
                {
                   jsonstr = reader.ReadToEnd();
                   respstream.Dispose();
                }
            }
            catch (Exception ex)
            {
                ErroLog.SysLog(ex);
            }
            return jsonstr; 
        }





        public class Rootobject
        {
            [JsonProperty("status")]
            public int Status { get; set; }

            [JsonProperty("message")]
            public string Message { get; set; }

            [JsonProperty("result")]
            public Result[] Result { get; set; }
        }

        public class Result
        {
            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("uid")]
            public string Uid { get; set; }

            [JsonProperty("city")]
            public string City { get; set; }

            [JsonProperty("district")]
            public string District { get; set; }

            [JsonProperty("business")]
            public string Business { get; set; }

            [JsonProperty("cityid")]
            public string Cityid { get; set; }

            [JsonProperty("location")]
            public Location Location { get; set; }

        }

        public class Location
        {
            [JsonProperty("lat")]
            public float Lat { get; set; }

            [JsonProperty("lng")]
            public float Lng { get; set; }
        }




        public class CityRoot
        {
            [JsonProperty("address")]
            public string address { get; set; }

            [JsonProperty("content")]
            public Content content { get; set; }

            [JsonProperty("status")]
            public int status { get; set; }
        }

        public class Content
        {
            [JsonProperty("address")]
            public string Address { get; set; }

            [JsonProperty("address_detail")]
            public Address_Detail Addressdetail { get; set; }

            [JsonProperty("point")]
            public Point Point { get; set; }
        }

        public class Address_Detail
        {
            [JsonProperty("city")]
            public string City { get; set; }

            [JsonProperty("city_code")]
            public int City_code { get; set; }

            [JsonProperty("district")]
            public string District { get; set; }

            [JsonProperty("province")]
            public string Province { get; set; }

            [JsonProperty("street")]
            public string Street { get; set; }

            [JsonProperty("street_number")]
            public string StreetNumber { get; set; }
        }

        public class Point
        {
            [JsonProperty("x")]
            public string X { get; set; }

            [JsonProperty("y")]
            public string Y { get; set; }
        }

    }
}
